package com.exercise.widgets.textview

import android.content.Context
import android.util.AttributeSet
import android.view.animation.Animation
import android.view.animation.LinearInterpolator
import android.view.animation.RotateAnimation
import androidx.appcompat.widget.AppCompatImageView

class RotateImageView @JvmOverloads constructor (
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : AppCompatImageView(context, attrs, defStyleAttr) {

    private var mRotateAnimation: RotateAnimation? = null

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        if (mRotateAnimation == null) {
            mRotateAnimation = RotateAnimation(
                0f,
                360f,
                (w / 2).toFloat(),
                (h / 2).toFloat()
            ).apply {
                duration = 1000
                repeatCount = Animation.INFINITE
                repeatMode = RotateAnimation.RESTART
                interpolator = LinearInterpolator()
            }
        }

        clearAnimation()
        startAnimation(mRotateAnimation)

    }
}